﻿#pragma once
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
#include "oatpp/web/server/api/ApiController.hpp"
#include "oatpp/web/mime/multipart/Multipart.hpp"
#include "oatpp/core/macro/codegen.hpp"
#include "../dto/UserDto.h"

#include OATPP_CODEGEN_BEGIN(ApiController)

class MyController : public oatpp::web::server::api::ApiController {
public:
	MyController(const std::shared_ptr<ObjectMapper>& objectMapper)
	: oatpp::web::server::api::ApiController(objectMapper) 
	{}

	ENDPOINT("GET", "/", root) {

		/* create user */
		auto user = User::createShared();
		user->name = "Ivan";
		user->surname = "Ovsyanochka";
		user->age = 24;
		user->familyMembers = {}; // Initialize empty map.
		user->additionalNotes = {}; // Initialize empty map.

		/* create user */
		auto brother = User::createShared();
		brother->name = "Yuriy";
		brother->surname = "Ovsyanochka";
		brother->age = 30;

		/* create user */
		auto sister = User::createShared();
		sister->name = "Kate";
		sister->surname = "Ovsyanochka";
		sister->age = 20;

		/* create list of siblings */
		oatpp::List<oatpp::Object<User>> siblings = { brother, sister };

		user->familyMembers->push_back({ "siblings", siblings });
		user->additionalNotes->push_back({ "Education", "Master of Computer Science" });

		oatpp::String json = m_defaultObjectMapper->writeToString(user);
		OATPP_LOGD("json", "value='%s'", json->c_str());

		auto cloneOfUser = m_defaultObjectMapper->readFromString<oatpp::Object<User>>(json);

		/* create beautiful mapper */
		/* create serializer and deserializer configurations */
		auto serializeConfig = oatpp::parser::json::mapping::Serializer::Config::createShared();
		auto deserializeConfig = oatpp::parser::json::mapping::Deserializer::Config::createShared();

		/* enable beautifier */
		serializeConfig->useBeautifier = true;

		/* create json object mapper with serializer config */
		auto jsonObjectMapper = oatpp::parser::json::mapping::ObjectMapper::createShared(serializeConfig, deserializeConfig);

		return createDtoResponse(Status::CODE_200, user);
		//return createResponse(Status::CODE_200, "Hello World!");
	}

	ENDPOINT("POST", "demo/api/json", postJson,
		BODY_DTO(User::ObjectWrapper, dto)) {
		auto name = dto->name;
		return createResponse(Status::CODE_200, "dtoMessage: " + name);
	}
  
	ENDPOINT("GET", "/users/{userId}", getUserById,
		PATH(Int64, userId))
	{
		return createResponse(Status::CODE_200, "OK");
	}
  
	ENDPOINT("GET", "/users/{my-path-variable}", getUserById2,
		PATH(Int64, userId, "my-path-variable"))
	{
		return createResponse(Status::CODE_200, "OK");
	}
};

#include OATPP_CODEGEN_END(ApiController)